let city = []
$.ajax({
  url: "../city.json",
  data: { action: "jQueryAjax" },
  type: "GET",
  async: false,
  success: function (data) {
    city = data
  },
  //返回数据的格式
  dataType: "json"
})

const enterpriseVue = new Vue({
  el: '#app',
  data() {
    return {
      pickerOptions: pickerOptions,
      rules: {
        Store_Name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        Opened_Date: [{ required: true, message: '请选择商品品牌', trigger: 'blur' }],
        Closed_Date: [{ required: true, message: '请选择商品类别', trigger: 'blur' }],
        Store_Type: [{ required: true, message: '请选择门店类别', trigger: 'change' }],
      },
      fileList2: [{ name: 'food.jpeg', url: '../../../../admin/images/act.jpg' }],
      // 整个省市县数据
      city,
      value: [],//地址
      address: '',
      enterprise_appid: [],
      // 被选中的市数据
      cityList: [],
      // 被选中的县数据
      areaList: [],
      selectProvince: {},
      selectCity: {},
      selectArea: {},
      dialogImageUrl: '',
      dialogVisible: false,
      form: {
        Store_SerialNumber: '',
        Store_Name: '',
        Opened_Date: '',
        Closed_Date: '',
        Store_Type: '',
        Store_Address: '',
        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
      },
      latitude: '',
      longitude: '',
      loading: false,
      map:null,
    }
  },
  mounted() {
    this.getData()
    this.initMap();
  },
  methods: {
    getData() {
      const _this = this
      $.ajax({
        url: `${DomainName}/getWeChatProgram?token=${$.cookie('token')}`,
        type: 'POST',
        data: {
          Enterprise_Info_SerialNumber: $.cookie('enterprise'),
        },
        success: function (res) {
          if (res.Status == 'success') {
            _this.enterprise_appid = res.Data
          }
        },
        complete: function (XMLHttpRequest) {
          _this.loading = false
          if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
            window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
          }
        }
      })
    },
    // a() {
    //   let _this = this
    //   $.ajax({
    //     url: DomainName + `/getAddressResolutionAction?Address=${this.form.Store_Address}&token=${$.cookie('token')}`,
    //     type: 'GET',
    //     success: function (res) {
    //       console.log('可以获取到精度和纬度', res)
    //       // let latitude=res.Data.lat
    //       // _this.latitude=latitude
    //       // console.log('latitude',_this.latitude)
    //       // const longitude=res.Data.lng
    //       // console.log('longitude',longitude)

    //       // _this.longitude=longitude
    //       // console.log(_this.longitude)

    //       // callback()
    //     },
    //     error: function () {
    //       _this.$message.error("操作失败，请稍后再试")
    //     },
    //     complete: function (XMLHttpRequest) {
    //       _this.loading = false
    //       if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
    //         window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
    //       }
    //     }
    //   })
    // },
    submitForm(formName) {
      let _this = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true
          // _this.a(() => {
            $.ajax({
              url: DomainName + '/getAddAction',
              type: 'POST',
              data: {
                Data: JSON.stringify({
                  store_info: [_this.form]
                }),
                token: $.cookie("token")
              },
              success: function (res) {
                console.log(res)
                _this.$message.success("提交成功")
                window.location.href = './Store.html'
              },
              error: function () {
                _this.$message.error("操作失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                _this.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          // })

        } else {
          console.log("error submit!!");
          return false;
        }

      });
    },
    handleChange(e) {
      this.form.Store_Address = `${this.value[0]}/${this.value[1]}/${this.value[2]}/${e}`
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 省份 市 县联动
    selectProvinceFun(event) {
      console.log(event)
      if (event) {
        this.cityList = event.children
      } else {
        this.cityList = []
      }
      this.areaList = []
    },
    selectCityFun(event) {
      console.log(event)
      if (event) {
        this.areaList = event.children
      } else {
        this.areaList = []
      }
    },
    selectAreaFun(event) {
      console.log(event)
      this.form.Store_Address = this.selectProvince.label + "/" + this.selectCity.label + "/" + this.selectArea.label
    },
    // 初始化地图
    initMap() {
      if (window.TMap) {
         this.map = new window.TMap.Map('tencent-map-container', {
          zoom: 15, // 初始缩放级别
          center: new window.TMap.LatLng(39.916527, 116.397128), // 初始中心经纬度（例如北京的位置）
        });
      } else {
        console.error('腾讯地图API未加载完成');
      }
    },
    // 店铺地址选择完成时地图也发生变化
    map1(value) {
        let _this = this
        $.ajax({
          url: DomainName + `/getAddressResolutionAction?Address=${this.value[0]}/${this.value[1]}/${this.value[2]}/${this.address}&token=${$.cookie('token')}`,
          type: 'GET',
          success: function (res) {
            console.log('可以获取到精度和纬度', res)
            let latitude=res.Data.lat
            _this.latitude=latitude
            const longitude=res.Data.lng
            _this.longitude=longitude
            console.log( _this.latitude,_this.longitude)
            let position = new TMap.LatLng(_this.latitude, _this.longitude);
            // 移动地图中心到指定位置
            _this.map.setCenter(position);
            let marker = null; 


            // 需要点击才能出现标点
            _this.map.on("click", (evt) => {
            // 清除之前的标点
            if (marker) {
              marker.setMap(null);
            }
            // 添加新的标点
            marker = new TMap.MultiMarker({
              map: _this.map,
              geometries: [{
                position: evt.latLng,
              }]
            });
            _this.form.longitude = evt.latLng.getLng().toFixed(6);
            _this.form.latitude = evt.latLng.getLat().toFixed(6);
            _this.getLocation();
            // 移动地图中心到新标点位置
            _this.map.setCenter(evt.latLng);
          });
          },
          error: function () {
            _this.$message.error("操作失败，请稍后再试")
          },
          complete: function (XMLHttpRequest) {
            _this.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
    },

  }
})